
package UFFT_package.chart1;
import p1.menu;

import UFFT_package.Decision1.Decision;
import UFFT_package.Node1.Node;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
import java.util.Enumeration;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

import p1.menu;
 
public class PieChartExample extends JComponent
{
	public static JInternalFrame f = new JInternalFrame("Statistics For UFFT");
	public PieChartExample()
    {
        
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new PieChartPanel());
        
        f.setSize(1000,800);
        f.setLocation(300,300);
        f.setVisible(true);
    }
}
 class PieChartPanel extends JPanel
{
    BufferedImage image;
    final int PAD = 30;
    Font font;
    NumberFormat numberFormat;
    
	public PieChartPanel()
    {
		System.out.println("pie UFFT");
		font = new Font("Book Antiqua", Font.PLAIN, 14);
        numberFormat = NumberFormat.getPercentInstance();
        addMouseListener(new Visibility(this));
        addComponentListener(new ComponentAdapter(){});
              
    }
 protected void paintComponent(Graphics graphics)
    {
        super.paintComponent(graphics);
        Graphics2D graphics2d = (Graphics2D)graphics;
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        createChartImage();
        graphics2d.drawImage(image, 0, 0, this);
    }
    private void createChartImage()
    {
        System.out.println("pie UFFT");
        int width=1000;
        int height=600;
    	
        int cp=700;
        int cq=150;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.white);
        g2.fillRect(0,0, width, height);
        g2.setPaint(Color.black);
        
        int pie=200;
        g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pie, pie));
        double total = 0;
        for(int j = 0; j < UFFT_package.Decision1.Decision.class_count.length; j++)
            total += Decision.class_count[j];
        double theta = 0, phi = 0;
        double p, q;
        for(int j = 0; j < Decision.class_count.length; j++)
        {
            p = cp + (pie/2) * Math.cos(theta);
            q = cq + (pie/2) * Math.sin(theta);
            g2.draw(new Line2D.Double(cp, cq, p, q));
            phi = (Decision.class_count[j]/total) * 2 * Math.PI;
            p = cp + (9*pie/24) * Math.cos(theta + phi/2);
            q = cq + (9*pie/24) * Math.sin(theta + phi/2);
            g2.setFont(font);
            String st = String.valueOf(Decision.class_count[j]);
            FontRenderContext frc = g2.getFontRenderContext();
            float textWidth = (float)font.getStringBounds(st, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(st, frc);
            float sp = (float)(p - textWidth/2);
            float sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st, sp, sq);
            p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
            q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
            String st1 = Decision.classes[j];
            textWidth = (float)font.getStringBounds(st, frc).getWidth();
            lm = font.getLineMetrics(st, frc);
            sp = (float)(p - textWidth/2);
            sq = (float)(q + lm.getAscent()/2);
            g2.drawString(st1, sp, sq);
            theta += phi;
        }
       
        font = new Font("Book Antiqua",Font.PLAIN, 14);
        g2.setFont(font);
        g2.drawString("Number Of Classes ::"+Decision.num_classes,50,50);
        g2.drawString("Number of Examples ::"+Decision.num_examples, 50, 70);
        g2.drawString("Majority Class Label::"+Decision.classes[Decision.majority_class], 50, 90);
        
        font = new Font("Book Antiqua",Font.BOLD, 14);
        g2.setFont(font);
        g2.drawString("Class Distribution ::", 50, 130);
        g2.drawString("Classes ",50,150);
    	g2.drawString("Label",150,150);
    	g2.drawString("Proportion",250,150);
    	font = new Font("Book Antiqua",Font.PLAIN, 14);
     	g2.setFont(font);
        for(int i=0;i<Decision.num_classes;i++)
        {
        	float per=((float)Decision.class_count[i]/(float)Decision.num_examples)*100;
        	
        	g2.drawString(" "+i,50,170+(20*i));
        	g2.drawString(Decision.classes[i],150,170+(20*i));
        	g2.drawString(per+"%",250,170+(20*i));
        }
        
        font = new Font("Book Antiqua",Font.BOLD, 14);
        g2.setFont(font);
        g2.drawString("Tree Information",80,270);
        DefaultMutableTreeNode t1=UFFT_package.Decision1.Decision.top;
       
        Enumeration en;
        en=t1.depthFirstEnumeration();
        int numNodes=0,numLeaf=0;
        while(en.hasMoreElements())
        {
        	numNodes++;
        	t1=(DefaultMutableTreeNode) en.nextElement();
        	if(t1.isLeaf())
        		numLeaf++;
        }
        font = new Font("Book Antiqua",Font.PLAIN, 14);
        g2.setFont(font);
        g2.drawString("Number of Nodes : "+numNodes,50,290);
        g2.drawString("Number of Leaf Nodes : "+numLeaf,50,310);
        g2.drawString("Number of Levels : "+Decision.top.getDepth(),50,330);
        
        int i=0,j=0;
	    if(menu.confusion_testing2!=null)
	    {	
	    	font = new Font("Book Antiqua",Font.BOLD, 14);
	        g2.setFont(font);
	        g2.drawString("Confusion Matrix : ",50,370);
	        g2.drawString("Predicted Class Label",80,390);
	        g2.drawString("Actual",20,410);
	        g2.drawString("Class",20,430);
	        g2.drawString("Label",20,450);
	    	for(i=0;i<Decision.num_classes;i++)
	        {
	        	g2.drawString(Decision.classes[i],140+20+(60*i),410);
	        	g2.drawString(Decision.classes[i],80,440+(30*i));
	        	for(j=0;j<Decision.num_classes;j++)
	        	{
	        			g2.drawString(""+menu.confusion_testing2[i][j],145+20+(60*j),440+(30*i));
	        	}
	        }
	    	g2.drawRect(65,400,80*i+50,35*i);
	    	g2.drawLine(65,420,65+(80*i+50),420);
	    	g2.drawLine(122+20,400,122+20,400+(35*i));
	    }
	    font = new Font("Book Antiqua",Font.PLAIN, 14);
        g2.setFont(font);
	    float per=((float)menu.misclassified2/(float)menu.num_records)*100;
	    g2.drawString("Number of Test Observation:     "+menu.num_records,500,380);
	    g2.drawString("% Classified:     "+(100-per),500,420);
	    g2.drawString("% Misclassified:     "+per,500,400);
	    
	    g2.drawString("Time Taken to build the Decision Tree: "+menu.duration+" miliseconds",500,450);
	    g2.drawString("Menory Consumed to store the Decision Tree: "+menu.overhead+" bits",500,480);
        g2.dispose();
    }
 public void toggleVisibility()
    {
        repaint();
    }
}
class Visibility extends MouseAdapter
{
    PieChartPanel piechart;
public Visibility(PieChartPanel pc)
    {
        piechart = pc;
    }
 public void mousePressed(MouseEvent event)
    {
        if(event.getClickCount() > 1)
            piechart.toggleVisibility();
    }
}